﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quick.CodeGenerator.Model;

namespace Quick.CodeGenerator.CodeTemplate
{
    public class EntityTemplate : ITemplate
    {
        /// <summary>
        /// 生成模版
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        public string GenerateTemplate(CodeGeneratorSetting setting, string tableName, List<Model.ColumnInfo> columns)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendLine($"/****************");
            strBuilder.AppendLine($"* Creat Date: {DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")}");
            strBuilder.AppendLine($"* Creator: {setting.Creator}");
            strBuilder.AppendLine($"*****************/");
            strBuilder.AppendLine($"using System;");
            strBuilder.AppendLine($"using SqlSugar;");
            strBuilder.AppendLine($"namespace {setting.Project}.{setting.EntityNamespace}");
            strBuilder.AppendLine("{");
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 数据表实体类：" + tableName);
            strBuilder.AppendLine("    /// </summary>");
            strBuilder.AppendLine("    [Serializable]");
            strBuilder.AppendLine($"    public partial class {tableName} : Shared.Contracts.EntityBase");
            strBuilder.AppendLine("    {");
            //取表栏位字段属性
            foreach (Model.ColumnInfo ci in columns)
            {
                strBuilder.AppendLine($"        private {ci.CSharpColumnType} _{ci.COLUMN_NAME};");
                strBuilder.AppendLine($"        /// <summary>");
                strBuilder.AppendLine($"        /// {Convert.ToString(ci.COMMENTS+"").Trim().Replace("  ","").Replace("\r", "\n        /// ").Replace("\n", "\r\n        /// ")}");
                strBuilder.AppendLine($"        /// </summary>");
                if (ci.CONSTRAINT_TYPE == "P")
                {
                    strBuilder.AppendLine("        [SugarColumn(IsPrimaryKey = true)]");
                }
                strBuilder.AppendLine($"        public {ci.CSharpColumnType} {ci.COLUMN_NAME} ");
                strBuilder.AppendLine( "        {");
                strBuilder.AppendLine( "             get { return _" + ci.COLUMN_NAME + "; }");
                strBuilder.AppendLine( "             set");
                strBuilder.AppendLine( "             {");
                if (ci.CONSTRAINT_TYPE == "P")
                {
                    strBuilder.AppendLine($"                AddPKChange(\"{ci.COLUMN_NAME}\", _{ci.COLUMN_NAME}, value);");
                }
                else
                {
                    strBuilder.AppendLine($"                AddPropertyChange(\"{ci.COLUMN_NAME}\");");
                }
                strBuilder.AppendLine($"                _{ci.COLUMN_NAME} = value;");
                strBuilder.AppendLine( "             }");
                strBuilder.AppendLine( "        }");
            }
            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");
            return strBuilder.ToString();
        }
    }
}